<?php

@include_once("../config.php");
include_once(DAO."/Conexao.php");
include_once(ENTIDADE."/Admin.php");

/**
 * Classe AdminDAO
 *
 * @author Falci
 * @package dao
 */
class AdminDAO {

    public function  __construct() {
        new Conexao();
    }

    public function inserir($admin){
        $sql = "INSERT INTO admin VALUES(0,'".$admin->getNome()."','".$admin->getSenha()."',".$admin->getStatus().");";
        return mysql_query($sql);
    }

    public function alterar($admin){
        $sql = "UPDATE admin SET nome='".$admin->getNome()."', senha='".$admin->getSenha()."' WHERE id=".$admin->getId().";";
        return mysql_query($sql);
    }

    public function excluir($admin){
        $sql = "UPDATE admin SET status=0 WHERE id=".$admin->getId().";";
        return mysql_query($sql);
    }

    public function listar(){
        $array = Array();
        $query = mysql_query("SELECT * FROM admin WHERE status=1 ORDER BY nome");
        while ($row = mysql_fetch_object($query)) {
            $admin = new Admin;
            $admin->setId($row->id);
            $admin->setNome($row->nome);
            $admin->setSenha($row->senha, false);
            $admin->setStatus($row->status);
            $array []= $admin;
        }

        return $array;
    }

    public function buscaIgual($admin) {
        if($admin->getId() == null) $admin->setId(0);

        $sql = "SELECT id FROM admin WHERE id<>".$admin->getId()." AND nome='".$admin->getNome()."' AND status=1";
        $query = mysql_query($sql);
        if(mysql_num_rows($query) == 0)
            return false;
        else
            return true;
    }

    public function pesquisar($q){
        $array = Array();
        $query = mysql_query("SELECT * FROM admin WHERE status=1 AND nome like '%$q%' ORDER BY nome");
        while ($row = mysql_fetch_object($query)) {
            $admin = new Admin;
            $admin->setId($row->id);
            $admin->setNome($row->nome);
            $admin->setSenha($row->senha, false);
            $admin->setStatus($row->status);
            $array []= $admin;
        }

        return $array;
    }

    public function login($nome,$senha){
        session_start();
        $admin = new Admin;
        $admin->setNome($nome);
        $admin->setSenha($senha);
        $id = $this->verificarSenha($admin);
        if($id){
            //$admin->setId($id);
            $_SESSION['login']['admin'] = $id;
            $_SESSION['login']['time'] = time();
            return true;
        } else {
            unset($_SESSION['login']);
            return false;
        }

    }

    public function verificarSenha($admin){
        $query = "SELECT id FROM admin WHERE nome='".$admin->getNome()."' AND senha='".$admin->getSenha()."';";
        $result = mysql_query($query);
        if(mysql_num_rows($result) == 1){
            return mysql_result($result, 0);
        } else {
            return false;
        }
    }

    public function buscaPorId($id){
        $admin = new Admin;
        $query = mysql_query("SELECT * FROM admin WHERE id=$id;");
        while ($row = mysql_fetch_object($query)) {
            $admin->setId($row->id);
            $admin->setNome($row->nome);
            $admin->setSenha($row->senha, false);
        }
        return $admin;
    }
}
?>